package com.soccertgc.core.game
{
	import com.soccertgc.core.Position;
	import com.soccertgc.utils.ClassUtils;
	
	public class GameAction
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------              
		
		public static const PASS:GameAction						= new GameAction(1, "pass", 	"d1;d2;mf;f1;f2",	"");
		public static const DRIBBLE:GameAction                	= new GameAction(2, "dribble", 	"d1;d2;mf;f1;f2",	"");
		public static const CROSS:GameAction                	= new GameAction(3, "cross", 	"f1;f2",			"");
		public static const SHOOT:GameAction                	= new GameAction(4, "shoot", 	"f1;f2",			"");
		
		// -------------------------------------------------------------------------------
		// STATIC
		// -------------------------------------------------------------------------------
		
		private static var _initialized:Boolean = false;
		private static var _list:Vector.<GameAction>;
		
		// Fake static constructor
		{
			// Fill the internal list
			_list = new Vector.<GameAction>();
			_list.push(GameAction.PASS);
			_list.push(GameAction.DRIBBLE);
			_list.push(GameAction.CROSS);
			_list.push(GameAction.SHOOT);
			// Init!
			_initialized = true;
		}
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _id:uint;
		private var _name:String;
		private var _positions:Vector.<Position>;	// posizioni da cui l'azione è giocabile
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function GameAction(id:uint, name:String, positions:String, targets:String)
		{
			// Safe unique instance lock
			if (_initialized) throw new Error("GameAction enum already created!");
			// Init
			_id    = id;
			_name  = name;
			_positions = new Vector.<Position>();
			// Position list parsing and setup
			var pos:Array = positions.split(";");
			for (var i:int = 0; i < pos.length; i++)
			{
				_positions.push(Position.getBySign(pos[i]));
			}
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------              
		
		public function get id():uint
		{
			return _id;
		}
		
		public function get name():String
		{
			return _name;
		}
		
		public function get positions():Vector.<Position>
		{
			return _positions;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * Check if an action type is allowed in a given position 
		 * @param position
		 * @return 
		 * 
		 */		
		public function allowed(position:Position):Boolean
		{
			return _positions.indexOf(position) > -1;
		}
		
		public function toString():String
		{
			return ClassUtils.getName(this) + "." + _name;
		}
		
		// -------------------------------------------------------------------------------
		// STATIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * Returns the list of the names of the game sides
		 * @return List of the names of the game sides
		 */
		public static function names():Array
		{
			var list:Array  = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i].name);
			}
			return list;
		}
		
		/**
		 * 
		 * @return 
		 */
		public static function list():Array
		{
			var list:Array = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i]);
			}
			return list;
		}
		
		/**
		 * Search a GameAction by given id 
		 * @param id Id of the log target to find
		 * @return GameAction constant for the given id, null if no GameAction
		 * for the given id has been found
		 */             
		public static function getById(id:uint):GameAction
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].id == id) return _list[i];
			}
			throw new Error("GameAction for id '" + id + "' unknown!");
		}
		
		/**
		 * Search a GameAction by given name 
		 * @param name Name of the log target to find
		 * @return GameAction constant for the given id, null if no GameAction
		 * for the given name has been found
		 */             
		public static function getByName(name:String):GameAction
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].name == name) return _list[i];
			}
			throw new Error("GameAction for name '" + name + "' unknown!");
		}
				
	}
	
}